$.fn.autoChange = function(config) {
   var options = $.extend({
      effect  : 'fade',
      type    : 'repaet',
      timeout : 3000,
      speed   : 1000,
      object  : null
   }, config);

   return this.each(function() {
      var current = 0;
      var next = 1;

      var element = $(this).children();

      $(element).hide();

      $(element[0]).show();

      var change = function(page) {
         if($(element[current]).is(':hover') && !page){
             return;
         }
         if( page > 0 ) {
            next=page-1;
         }
         if (options.effect == 'fade') {
            $(element[current]).css('display', 'none');
            $(element[next]).css('opacity', 0);
            $(element[next]).css('display', 'block');
            $(element[next]).animate({'opacity': 1}, 400, 'linear');

         } else if  (options.effect == 'slide') {
            $(element[current]).slideUp(options.speed);
            $(element[next]).slideDown(options.speed);
         }

         if (options.type == 'repeat') {
            if ((next + 1) < element.length) {
                current = next;
                next++;
            } else {
                current = element.length - 1;
                next = 0;
            }
         }

         if (options.type == 'stop') {
            if ((next + 1) < element.length) {
                current = next;
                next++;
            } else {
                return;
            }
         }
      };
      var timer = setInterval(function(){change();}, options.timeout);
      if(options.object) {
          options.object.click(function() {
              if($(this).attr('j-page') > 0) {
                  clearInterval(timer);
              }
              change($(this).attr('j-page'));
              timer = setInterval(function(){change();}, options.timeout);
          });
      }
   });
};
$.fn.prevNextList = function(config) {
    var options = $.extend({
          speed   : 800,
          per_page : 5,
          object_next  : null,
          object_prev  : null,
          object_container  : null,
          object_item_absolute : null
       }, config);
    if(!options.object_next || !options.object_prev || !options.object_container || !options.object_item_absolute) return;
    var move_range = options.object_container.width() - options.object_item_absolute.position().left;
    options.object_next.click(function() {
        var pos_x = options.object_item_absolute.position().left - move_range;
        pos_x = Math.min(Math.abs(pos_x), options.object_item_absolute.width() - 5 - move_range);
        options.object_item_absolute.animate({
            'left' : '-' + pos_x + 'px'
        }, options.speed, function() {
            
        });
    });
    options.object_prev.click(function() {
        var pos_x = options.object_item_absolute.position().left + move_range;
        pos_x = Math.min(pos_x, 5);
        options.object_item_absolute.animate({
            'left' : pos_x + 'px'
        }, options.speed, function() {
            
        });
    });
};
$(function() {
   $('.slide-autochange').autoChange({effect : 'fade', type : 'repeat', timeout: 3000, object: $('.slide-autochange a.change-bg')});
});
